package com.cognizant.ams.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.cognizant.ams.bean.Ldmenu;
import com.cognizant.ams.bean.Ldmenugrp;
import com.cognizant.ams.common.PageHelper;
import com.cognizant.ams.service.MenuRoleService;
import com.cognizant.ams.service.MenuService;
import org.apache.juli.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping("/role")
public class RoleController {

    private final static Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private MenuRoleService roleService;
    @Autowired
    private MenuService menuService;
    private static String message="";

    @GetMapping("/redirect")
    public ModelAndView redirect(String page,String id){
        ModelAndView modelAndView=new ModelAndView("/role/"+page);
        modelAndView.addObject("id",id);
        return  modelAndView;
    }

    @GetMapping("/query")
    public ModelAndView getAllrole(String cur,String menugrpcode,String menugrpname){
       if (null==cur){
           cur="1";
       }
        ModelAndView modelAndView =new ModelAndView();
       modelAndView.setViewName("role/role");

       Ldmenugrp role=new Ldmenugrp();
       role.setMenugrpcode(menugrpcode);
       role.setMenugrpname(menugrpname);
       List<Ldmenugrp> roleList=roleService.getOneRole(role);

        int pageCount=0;
        int pageSize=10;
        if (roleList!=null&&roleList.size()>0) {
            pageCount=roleList.size()/pageSize+1;
            roleList= PageHelper.getDataByFenye(roleList, cur,pageSize);
        }
        else {
            cur="0";
        }
        //设置返回视图
        //设置返回参数
        //分页数据
        modelAndView.addObject("roleList",roleList);
        modelAndView.addObject("pageCount", pageCount);
        modelAndView.addObject("cur", Integer.parseInt(cur));
        //参数回传
        modelAndView.addObject("nodecode", menugrpcode);
        modelAndView.addObject("nodename", menugrpname);

        return  modelAndView;
    }

    @PostMapping("/add")
    protected  ModelAndView addRole(String menugrpcode,String menugrpname,String menugrpdescription){
        ModelAndView modelAndView =new ModelAndView();
      Ldmenugrp role=new Ldmenugrp();
      role.setMenugrpcode(menugrpcode);
      role.setMenugrpname(menugrpname);
      role.setMenugrpdescription(menugrpdescription);
      role.setOperator("admin");
        int result=0;
        try{
          result=roleService.addRole(role);
          logger.info("新增角色{}{}条",menugrpcode,result);
          if (result>0){
              message="角色添加成功！";

          }else {
              message="角色添加失败";
          }
      }catch(Exception e){
          e.printStackTrace();
          message="角色增失败"+e.getMessage();
      }
        //设置返回视图
        modelAndView.setViewName("role/roleadd");
        //设置返回参数

        modelAndView.addObject("code", result);
        modelAndView.addObject("message", message);

        return  modelAndView;


    }

    @PostMapping("/delete")
    @ResponseBody
    public int removeRole(String rolecode,String rolename){
        ModelAndView modelAndView=new ModelAndView();
        int result=0;
        try {
            result=roleService.deleteRole(rolecode);
            logger.info("删除角色{}{}条",rolecode,result);
            if (result>0){
                message="角色删除成功！";

            }else {
                message="角色删除失败";
            }
        }catch (Exception e){
            e.printStackTrace();
            message="角色删除失败";
        }

        //设置返回视图
        modelAndView.setViewName("role/role");
        //设置返回参数

        modelAndView.addObject("code", result);
        modelAndView.addObject("message", message);

        return  result;
    }



    @PostMapping("/update")
    public ModelAndView updateRole(String menugrpcode,String menugrpname,String menugrpdescription,String operator){
        ModelAndView modelAndView=new ModelAndView();
        Ldmenugrp role=new Ldmenugrp();
        role.setMenugrpcode(menugrpcode);
        role.setMenugrpname(menugrpname);
        role.setMenugrpdescription(menugrpdescription);
        role.setOperator(operator);
        int result=0;
        try {
            result=roleService.updateRole(role);
            if (result>0){
                message="角色更新成功！";

            }else {
                message="角色更新失败";
            }
        }catch (Exception e){
            e.printStackTrace();
            message="角色更新失败";
        }

        //设置返回视图
        modelAndView.setViewName("role/roleupdate");
        //设置返回参数

        modelAndView.addObject("result", result);
        modelAndView.addObject("message", message);

        return  modelAndView;
    }



    @GetMapping(value = "/rightMenus" )
    @ResponseBody
    public String getRoleRight(String rolecode){
         List<Ldmenu> allMenus= menuService.getAllMenu();
        String menusJson=JSONObject.toJSONString(allMenus);

       List<Ldmenu> rightMenus= roleService.getMenusbyRole(rolecode);
       String rightMenusJson=JSONObject.toJSONString(rightMenus);

      JSONObject returnJson= new JSONObject();
      returnJson.put("menuJson",allMenus);
      returnJson.put("rightMenusJson",rightMenus);
       return returnJson.toJSONString();
    }



    @PostMapping("/updateRoleRight")
    public int updateRoleRight(String rolecode,String menusJSON){
        return 0;
    }
}
